package com.haochang.chunkvod.controller.activity.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshBase;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshListView;
import com.haochang.chunkvod.view.vod.dialog.AddReserveRoomDialog;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 *     @author Yangjie
 *     className ReserveActivity
 *     created at  2016/8/23  18:08
 *     咨客 现场排号
 */
public class LiveARowNumberActivity extends BaseActivity
{

	private TitleView titleView;
	private PullToRefreshListView listView;
	private HostessData hostessData;
	private QuickAdapter<HostessInfo> adapter;
	private List<HostessInfo> hostessInfoList = new ArrayList<HostessInfo>();
	private List<HostessInfo> roomTypeList;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{
	}

	@Override
	public void initData()
	{
		hostessData = new HostessData(LiveARowNumberActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_live_a_row_number);
		Button btn_call = (Button) findViewById(R.id.btn_call);
		Button btn_sure = (Button) findViewById(R.id.btn_sure);
		titleView = (TitleView) findViewById(R.id.titleView);
		listView = (PullToRefreshListView) findViewById(R.id.listView);
		btn_call.setOnClickListener(onBaseClickListener);
		btn_sure.setOnClickListener(onBaseClickListener);
		titleView.setTitle(R.string.live_a_row_num).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("回到首页");
				list.add("修改密码");
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(LiveARowNumberActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							backToHome();
							break;
						case 1:
							goToPasswordModify();
							break;
						case 2:
							loginOut();
							break;

						default:
							break;
						}
					}
				});
			}
		});
	}

	@Override
	public void initViewsData()
	{
		super.initViewsData();
		adapter = new QuickAdapter<HostessInfo>(this, R.layout.item_reserve_room, hostessInfoList)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				helper.setText(R.id.tv_room_num, item.getRoomNum());
				helper.setText(R.id.tv_date, item.getStart_time());
				helper.setText(R.id.tv_nick, item.getAreaName());//顾客nick，用getAreaName代替
				helper.setText(R.id.tv_reserver, item.getName());
			}
		};
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<BaseListView>()
		{
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{
				requestLivaARawNumberList();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{

			}
		});
		requestRoomTypeList();//获取 新增预留房间时的房型列表
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_call:
				//叫号
				break;
			case R.id.btn_sure:
				if (CollectionUtils.isEmpty(roomTypeList))
				{
					ToastUtils.showText(LiveARowNumberActivity.this, "正在获取房型列表...");
					requestRoomTypeList();
					return;
				}
				//新增
				new AddReserveRoomDialog.Builder(LiveARowNumberActivity.this).setTitle("新增现场排号").setDataList(roomTypeList)
						.setOnResultListener(new AddReserveRoomDialog.OnResultListener()
						{
							@Override
							public void onResult(HostessInfo info, int number)
							{
								requestAddLiveARowNumber(info.getRoomType(),number);
							}
						}).showDialog(4);
				break;
			}
		}
	};

	/**
	 * 获取预约房间list
	 */
	@SuppressWarnings("unchecked")
	private void requestLivaARawNumberList()
	{
		hostessData.requestLivaARawNumberList("", new HostessData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				listView.onRefreshComplete();
				if (!CollectionUtils.isEmpty(list))
				{
					hostessInfoList.clear();
					adapter.replaceAll(list);
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				listView.onRefreshComplete();
				ToastUtils.showText(LiveARowNumberActivity.this, TextUtils.isEmpty(errMsg) ? "获取现场排号列表失败!" : errMsg);
			}
		});
	}

	/**
	 * 获取房型list
	 */
	@SuppressWarnings("unchecked")
	private void requestRoomTypeList()
	{
		hostessData.requestRoomTypeList("", new HostessData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					roomTypeList = list;
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
			}
		});
	}

	/**
	 * 请求添加 现场排号
	 */
	private void requestAddLiveARowNumber(String roomType,int count)
	{
		hostessData.requestAddLiveARowNumber(roomType,count, new HostessData.ResultResponse()
		{
			@Override
			public void onRequestSuccess()
			{
				ToastUtils.showText(LiveARowNumberActivity.this,"新增现场排号成功!");
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				ToastUtils.showText(LiveARowNumberActivity.this, TextUtils.isEmpty(errMsg) ? "添加预留房失败!" : errMsg);
			}
		});
	}
}
